package pikes.peak;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import pikes.html.xhtml.Flow;
import pikes.html.xhtml.PCData;
import pikes.html.xhtml.table.TD;
import pikes.html.xhtml.table.TR;
import pikes.html.xhtml.table.TableCell;
import pikes.peak.CellDecorator;
import pikes.peak.Component;
import pikes.peak.Row;
import pikes.peak.test.AssertHtml;
import pikes.peak.test.PikesPeakComponentTestCase;

import org.springframework.web.servlet.support.RequestContext;

public class RowTestCase extends PikesPeakComponentTestCase {

	@Test
	public void simpleRow() throws Exception {
		Row row = new Row();

		TR actual = row.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		Assert.assertNull(actual);
	}

	@Test
	public void simpleRowWithOneComponent() throws Exception {
		Row row = new Row();
		row.addCell(new Component() {

			public Flow createHtml(Map model, RequestContext requestContext) throws Exception {
				return null;
			}

		});

		TR actual = row.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		Assert.assertNull(actual);
	}

	@Test
	public void simpleRowWithOneComponent1() throws Exception {
		Row row = new Row();
		row.addCell(new Component() {

			public Flow createHtml(Map model, RequestContext requestContext) throws Exception {
				return new PCData("boo");
			}

		});

		TR expected = new TR(new TD("boo"));

		TR actual = row.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void simpleRowWithComponentArray() throws Exception {
		Component[] compArray = new Component[] { new Component() {

			public Flow createHtml(Map model, RequestContext requestContext) throws Exception {
				return new PCData("boo");
			}

		} };
		Row row = new Row();
		row.addCell(compArray);

		TR expected = new TR(new TD("boo"));

		TR actual = row.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void simpleRowWithOneComponentWithDecorator() throws Exception {
		Row row = new Row();
		row.addCell(new Component() {

			public Flow createHtml(Map model, RequestContext requestContext) throws Exception {
				return new PCData("boo");
			}

		}, new CellDecorator() {

			public void decorateCell(TableCell cell, Map model, RequestContext requestContext) throws Exception {
				cell.setId("id");
			}

		});

		TD td = new TD("boo");
		td.setId("id");
		TR expected = new TR(td);

		TR actual = row.getValue(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

}
